import requests

# 创建一个session对象
session = requests.Session()

# 第一个请求的headers
headers1 = {
    "Host": "gw.m.163.com",
    "x-b3-sampled": "1",
    "user-ssn": "w1XpAl+Njym9KMBL2Xrl/rTvXqmWyflQIA00EvD+ces=",
    "user-c": "5oiR",
    "x-nr-trace-id": "1739180033615_10774942240_5E7B1008-C0B6-4520-BEBD-4F9C7B7D3D2A",
    "user-appid": "zbCHbGcGkGiMBRRJXmZMeA==",
    "user-agent": "NewsApp/113.2 iOS/12.5.7 (iPhone7,1)",
    "user-da": "6rtkqGiR/7SS62/IrsvDwbpV7nCHcp+ZAZH5Hjwp3nPK0B5M/eVfdcw/9xwptur8",
    "x-nr-sign": "e9ef65e0e38dd66732020503919e9bfb",
    "user-n": "6QlSYk8QXf0OQ4AWdVtY7/jhya3RJrFegeb7075ultGqMsUK4MKsGp2OB35xS259",
    "user-tk": "55WCYjV2JkbHSWFNZtWO9a7h1Fh/TSxfZ0t119HBC6zPM8Dk+m0RDcsR2w0lMIFK",
    "user-yd": "fn/uO6GTtA+KpBJDjxG52lJinKcQaIQMalZTn2fYEBM=",
    "user-u": "w1XpAl+Njym9KMBL2Xrl/rTvXqmWyflQIA00EvD+ces=",
    "user-d": "4N9qgvC1bK9l8DqzoYQqeSVth/p5Q2QxRnSoliOJqQMx/eky7uQyrZO8tnPmOT7g",
    "accept-language": "zh-cn",
    "x-nr-ts": "1739180033617",
    "user-id": "X+w071U82P0uR4dec9fql2gFDfvQ83PEtgecsABxoZwpsh4p6A8WaUWKj1wX4M/plOBATdykwqnqwMiH88x4ht4ZRL0MIJQQ8KOb5fiSUJuMVMtqqnzmaVeVeFXjZ10SePBK0dNsyevylzp8V9OOiA==",
    "accept": "*/*",
    "user-sid": "a/YNOYHakSjQDR+wIN50HqkxYYr9jTjkAoSCvzmQd34S7hbbBGeP6xqkjFu67IJB",
    "x-b3-spanid": "0"
}

# 第二个请求的headers
headers2 = {
    "Host": "gw.m.163.com",
    "x-b3-sampled": "1",
    "user-ssn": "w1XpAl+Njym9KMBL2Xrl/rTvXqmWyflQIA00EvD+ces=",
    "user-c": "5oiR",
    "x-nr-trace-id": "1739180034657_10774846816_5E7B1008-C0B6-4520-BEBD-4F9C7B7D3D2A",
    "user-appid": "zbCHbGcGkGiMBRRJXmZMeA==",
    "user-agent": "NewsApp/113.2 iOS/12.5.7 (iPhone7,1)",
    "user-da": "6rtkqGiR/7SS62/IrsvDwbpV7nCHcp+ZAZH5Hjwp3nPK0B5M/eVfdcw/9xwptur8",
    "x-nr-sign": "d6e8d36df4686c431fd46eb6c9b2751a",
    "user-n": "6QlSYk8QXf0OQ4AWdVtY7/jhya3RJrFegeb7075ultGqMsUK4MKsGp2OB35xS259",
    "user-yd": "fn/uO6GTtA+KpBJDjxG52lJinKcQaIQMalZTn2fYEBM=",
    "user-tk": "55WCYjV2JkbHSWFNZtWO9a7h1Fh/TSxfZ0t119HBC6zPM8Dk+m0RDcsR2w0lMIFK",
    "user-u": "w1XpAl+Njym9KMBL2Xrl/rTvXqmWyflQIA00EvD+ces=",
    "user-d": "4N9qgvC1bK9l8DqzoYQqeSVth/p5Q2QxRnSoliOJqQMx/eky7uQyrZO8tnPmOT7g",
    "accept-language": "zh-cn",
    "x-nr-ts": "1739180034659",
    "user-id": "X+w071U82P0uR4dec9fql2gFDfvQ83PEtgecsABxoZwpsh4p6A8WaUWKj1wX4M/plOBATdykwqnqwMiH88x4ht4ZRL0MIJQQ8KOb5fiSUJuMVMtqqnzmaVeVeFXjZ10SePBK0dNsyevylzp8V9OOiA==",
    "accept": "*/*",
    "content-type": "application/octet-stream",
    "user-sid": "a/YNOYHakSjQDR+wIN50HqkxYYr9jTjkAoSCvzmQd34S7hbbBGeP6xqkjFu67IJB",
    "x-b3-spanid": "0"
}


def login_and_get_profile():
    try:
        # 第一个请求：登录
        login_url = "https://gw.m.163.com/commons-user-login/api/v2/commons/login/gateway/existedAccounts"
        login_response = session.get(login_url, headers=headers1)
        print("登录响应:")
        print(login_response.text)
        print(f"登录状态码: {login_response.status_code}")

        # 确认登录成功后继续
        if login_response.status_code == 200:
            # 第二个请求：获取用户资料
            profile_url = "https://gw.m.163.com/commons-user-main/api/v1/commons/user/profile/main"
            data = {
                "w1XpAl+Njym9KMBL2Xrl/rTvXqmWyflQIA00EvD+ces": ""
            }
            profile_response = session.post(profile_url, headers=headers2, data=data)
            print("\n获取用户资料响应:")
            print(profile_response.text)
            print(f"获取用户资料状态码: {profile_response.status_code}")
        else:
            print("登录失败，未执行获取用户资料的请求")

    except requests.exceptions.RequestException as e:
        print(f"请求发生错误: {e}")
    finally:
        # 关闭session
        session.close()


if __name__ == "__main__":
    login_and_get_profile()